/**********************************************
 * 
 * File Name				:	LimitSqlExecutor.java
 * 
 * Last Changed Date		: 	$Date: 2009-08-01 11:12:53 +0800 (星期六, 01 八月 2009) $
 * 
 * Last Changed Revision	:	$Revision: 86 $
 * 
 * Last Changed Author		:	$Author: cfanboy $
 * 
 * File Header URL			:	$HeadURL: svn://www.svnhost.cn/Busi-Building/code/branch/src/com/busibuilding/common/pagination/LimitSqlExecutor.java $
 * 
 * ********************************************
 */

package com.busibuilding.common.pagination;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ibatis.sqlmap.engine.execution.SqlExecutor;
import com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback;
import com.ibatis.sqlmap.engine.scope.StatementScope;

/**
 * Class Name: LimitSqlExecutor.java
 * Description: 
 * @author YU Zhipeng
 * @version 1.0
 * @since 1.0
 * Date: 2009-7-18
 */

public class LimitSqlExecutor extends SqlExecutor {
	private static final Log logger = LogFactory.getLog(LimitSqlExecutor.class);  
    
    private Dialect dialect;  

    private boolean enableLimit = true;  

    public Dialect getDialect() {  
        return dialect;  
    }  

    public void setDialect(Dialect dialect) {  
        this.dialect = dialect;  
    }  

    public boolean isEnableLimit() {  
        return enableLimit;  
    }  

    public void setEnableLimit(boolean enableLimit) {  
        this.enableLimit = enableLimit;  
    }   
    
	@Override
	public void executeQuery(StatementScope scope, Connection conn, String sql,
			Object[] parameters, int skipResults, int maxResults, RowHandlerCallback callback)
			throws SQLException {
		if ((skipResults != NO_SKIPPED_RESULTS || maxResults != NO_MAXIMUM_RESULTS)  
                && supportsLimit()) {  
            sql = dialect.getLimitString(sql, skipResults, maxResults);  
            if(logger.isDebugEnabled()){  
                logger.debug(sql);  
            }  
            skipResults = NO_SKIPPED_RESULTS;  
            maxResults = NO_MAXIMUM_RESULTS;              
        }    
        
		super.executeQuery(scope, conn, sql, parameters, skipResults, maxResults, callback);
	}
	
	public boolean supportsLimit() {  
        if (enableLimit && dialect != null) {  
            return dialect.supportsLimit();  
        }  
        return false;  
    }   
	
}
